package com.frxuexi.lib_base.mvi

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.app.TimePickerDialog.OnTimeSetListener
import android.os.Bundle
import android.widget.DatePicker
import android.widget.TimePicker
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import java.util.Calendar

abstract class BaseActivity<VDB:ViewDataBinding,UiIntent:IUiIntent,VM:BaseViewModel<UiIntent>>:AppCompatActivity() {

    abstract val binding:VDB
    abstract val vm:VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
        initData()
    }

    abstract fun initData()

    abstract fun initView()

    fun showDateDialog(date:(String)->Unit){
        val calendar = Calendar.getInstance()
        DatePickerDialog(this,object :OnDateSetListener{
            override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                val m=if(month+1<10) "0${month+1}" else "0${month+1}"
                val d=if(dayOfMonth<10) "0${dayOfMonth}" else "${dayOfMonth}"
                date("${year}-${m}-${d}")
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
    }

    fun showTimeDialog(time:(String)->Unit){
        TimePickerDialog(this,object :OnTimeSetListener{
            override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
                val h=if(hourOfDay<10) "0${hourOfDay}" else "${hourOfDay}"
                val m=if(minute<10) "0${minute}" else "${minute}"
                time("${h}:${m}:00")
            }
        },0,0,true).show()
    }
}